<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>批量下载素材 ZIP</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
        }
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            min-height: 100px;
            font-family: monospace;
        }
        .help-text {
            font-size: 12px;
            color: #888;
            margin-top: 5px;
        }
        button {
            background: #1890ff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background 0.3s;
        }
        button:hover {
            background: #40a9ff;
        }
        button:disabled {
            background: #d9d9d9;
            cursor: not-allowed;
        }
        .status {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .status.loading {
            background: #e6f7ff;
            border: 1px solid #91d5ff;
            color: #1890ff;
            display: block;
        }
        .status.success {
            background: #f6ffed;
            border: 1px solid #b7eb8f;
            color: #52c41a;
            display: block;
        }
        .status.error {
            background: #fff2e8;
            border: 1px solid #ffbb96;
            color: #ff4d4f;
            display: block;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>批量下载素材 ZIP</h1>

    <div class="form-group">
        <label for="materialIds">素材 ID 列表</label>
        <textarea id="materialIds" placeholder="每行输入一个素材 ID，例如：&#10;123&#10;456&#10;789"></textarea>
        <p class="help-text">请输入要下载的素材 ID，每行一个</p>
    </div>

    <button id="downloadBtn" onclick="handleDownload()">开始下载 ZIP</button>

    <div id="status" class="status"></div>
</div>

<script>
    function createHiddenIframe(downloadUrl, params, method = "POST") {
        let iframe = document.createElement('iframe');
        iframe.style.display = 'none';
        iframe.name = 'downloadIframe';
        document.body.appendChild(iframe);

        let form = document.createElement("form");
        form.style.display = "none";
        form.target = "downloadIframe";
        form.method = method;
        form.action = downloadUrl;

        if (params) {
            Object.keys(params).forEach((key) => {
                let value = params[key];
                let input = document.createElement("input");
                input.type = "hidden";
                input.name = key;
                input.value = value;
                form.appendChild(input);
            });
        }

        document.body.appendChild(form);
        form.submit();

        // 延迟清理，确保下载开始
        setTimeout(() => {
            document.body.removeChild(form);
            document.body.removeChild(iframe);
        }, 1000);
    }

    function handleDownload() {
        const materialIdsText = document.getElementById('materialIds').value.trim();
        const statusEl = document.getElementById('status');
        const downloadBtn = document.getElementById('downloadBtn');

        if (!materialIdsText) {
            statusEl.className = 'status error';
            statusEl.textContent = '请输入至少一个素材 ID';
            return;
        }

        // 解析ID列表
        const materialIdList = materialIdsText
            .split('\n')
            .map(id => id.trim())
            .filter(id => id && /^\d+$/.test(id))
            .map(id => parseInt(id, 10));

        if (materialIdList.length === 0) {
            statusEl.className = 'status error';
            statusEl.textContent = '请输入有效的数字 ID';
            return;
        }

        // 构建参数
        const params = {
            data: JSON.stringify({ materialIdList })
        };

        // 显示加载状态
        statusEl.className = 'status loading';
        statusEl.textContent = `正在准备下载 ${materialIdList.length} 个素材...`;
        downloadBtn.disabled = true;

        try {
            createHiddenIframe(
                'http://localhost:18885/datamanagement/material/download/zip',
                params
            );

            // 下载触发成功
            statusEl.className = 'status success';
            statusEl.textContent = '下载已开始，请查看浏览器下载列表';

            // 3秒后隐藏状态
            setTimeout(() => {
                statusEl.style.display = 'none';
            }, 3000);

        } catch (error) {
            statusEl.className = 'status error';
            statusEl.textContent = '下载失败：' + error.message;
        } finally {
            // 恢复按钮状态
            setTimeout(() => {
                downloadBtn.disabled = false;
            }, 1000);
        }
    }
</script>
</body>
</html>